package cn.iocoder.yudao.module.brokerage.record.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.basic.service.impl.MemberScopeService;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.brokerage.record.convert.BrokerageRecordConvert;
import cn.iocoder.yudao.module.brokerage.record.dao.BrokerageRecordMapper;
import cn.iocoder.yudao.module.brokerage.record.pojo.po.BrokerageRecordDO;
import cn.iocoder.yudao.module.brokerage.record.pojo.vo.AdminBrokerageRecordQueryDTO;
import cn.iocoder.yudao.module.brokerage.record.pojo.vo.AdminBrokerageRecordVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 分佣记录 Service 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class AdminBrokerageRecordServiceImpl extends MemberScopeService<BrokerageRecordDO, AdminBrokerageRecordVO, AdminBrokerageRecordQueryDTO> implements AdminBrokerageRecordService {
    @Resource
    private BrokerageRecordMapper brokerageRecordMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    protected BrokerageRecordMapper getInjectedMapper() {
        return brokerageRecordMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<BrokerageRecordDO> buildQueryWrapper(AdminBrokerageRecordQueryDTO param) {
        CoreQueryWrapper<BrokerageRecordDO> wrapper = super.buildQueryWrapper(param);
        wrapper.eq(StrUtil.isNotBlank(param.getBizType()), "biz_type", param.getBizType());
        wrapper.eqIfPresent("status", param.getStatus());
        wrapper.eqIfPresent("user_id", param.getUserId());
        wrapper.eqIfPresent("bonus_level", param.getBonusLevel());
        wrapper.orderByDesc("update_time");
        wrapper.orderByDesc("id");
        return wrapper;
    }

    @Nonnull
    @Override
    protected Map<Long, UserSimpleVO> getUserSimpleMap(Collection<Long> userIds) {
        return memberUserApi.getUserSimpleMap(userIds);
    }

    @Override
    public List<AdminBrokerageRecordVO> convertToViewObject(@Nonnull List<BrokerageRecordDO> entities) {
        if (CollUtil.isEmpty(entities)) {
            return Collections.emptyList();
        }
        List<AdminBrokerageRecordVO> vos = BrokerageRecordConvert.INSTANCE.convertList2(entities);
        Set<Long> userIds = new HashSet<>();
        userIds.addAll(convertSet(entities, BrokerageRecordDO::getUserId));
        userIds.addAll(convertSet(entities, BrokerageRecordDO::getSourceUserId));
        Map<Long, UserSimpleVO> users = getUserSimpleMap(userIds);
        for (AdminBrokerageRecordVO vo : vos) {
            vo.setUser(users.getOrDefault(vo.getUserId(), UserSimpleVO.ANONYMOUS));
            vo.setSourceUser(users.getOrDefault(vo.getSourceUserId(), UserSimpleVO.ANONYMOUS));
        }
        return vos;
    }

}